package com.ibeetl.admin.console.web;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.beetl.sql.core.engine.PageQuery;
import org.jxls.common.Context;
import org.jxls.util.JxlsHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.ibeetl.admin.console.service.LeaveMsgConsoleService;
import com.ibeetl.admin.console.service.OrgConsoleService;
import com.ibeetl.admin.console.service.PayOrderConsoleService;
import com.ibeetl.admin.console.service.RoleConsoleService;
import com.ibeetl.admin.console.service.UserConsoleService;
import com.ibeetl.admin.console.web.dto.LeaveMsgExcelExportData;
import com.ibeetl.admin.console.web.dto.PayOrderExcelExportData;
import com.ibeetl.admin.console.web.dto.UserExcelExportData;
import com.ibeetl.admin.console.web.query.LeaveMsgQuery;
import com.ibeetl.admin.console.web.query.PayOrderQuery;
import com.ibeetl.admin.console.web.query.UserQuery;
import com.ibeetl.admin.console.web.query.UserRoleQuery;
import com.ibeetl.admin.core.annotation.Function;
import com.ibeetl.admin.core.annotation.Query;
import com.ibeetl.admin.core.entity.CoreUser;
import com.ibeetl.admin.core.entity.CoreUserRole;
import com.ibeetl.admin.core.entity.LeaveMsg;
import com.ibeetl.admin.core.entity.PayOrder;
import com.ibeetl.admin.core.file.FileItem;
import com.ibeetl.admin.core.file.FileService;
import com.ibeetl.admin.core.service.CorePlatformService;
import com.ibeetl.admin.core.util.AnnotationUtil;
import com.ibeetl.admin.core.util.ConvertUtil;
import com.ibeetl.admin.core.util.PlatformException;
import com.ibeetl.admin.core.util.ValidateConfig;
import com.ibeetl.admin.core.util.enums.GeneralStateEnum;
import com.ibeetl.admin.core.web.JsonResult;

/**
 * 用户管理接口
 * 
 * @author xiandafu
 */
@Controller
public class PayOrderConsoleController {
	private final Log log = LogFactory.getLog(this.getClass());
	private static final String MODEL = "/admin/payorder";

	
	@Autowired
	PayOrderConsoleService payOrderConsoleService;

	@Autowired
	FileService fileService;
	

   

	/* 页面 */

	@GetMapping(MODEL + "/index.do")
	@Function("payorder")
	public ModelAndView index() {
		ModelAndView view = new ModelAndView("/admin/payorder/index.html");
		view.addObject("search", PayOrderQuery.class.getName());
		return view;
	}

	@GetMapping(MODEL + "/edit.do")
	@Function("payorder.edit")
	public ModelAndView edit(String id) {
		ModelAndView view = new ModelAndView("/admin/payorder/edit.html");
		PayOrder payOrder = payOrderConsoleService.queryPayOrderById(id);
		view.addObject("payOrder", payOrder);
		return view;
	}

	/* Json */

	@PostMapping(MODEL + "/delete.json")
	@Function("payorder.delete")
	@ResponseBody
	public JsonResult delete(String ids) {
		List<String> test = Arrays.asList(ids.split(","));

		payOrderConsoleService.batchDelPayOrder(test);
		return JsonResult.success();
	}

	@PostMapping(MODEL + "/view.json")
	@ResponseBody
	@Function("payorder.query")
	public JsonResult<PayOrder> view(String id) {
		PayOrder payOrder = payOrderConsoleService.queryPayOrderById(id);
		return JsonResult.success(payOrder);
	}

	@PostMapping(MODEL + "/list.json")
	@Function("payorder.query")
	@ResponseBody
	public JsonResult<PageQuery<PayOrder>> index(PayOrderQuery condtion) {

		PageQuery<PayOrder> page = condtion.getPageQuery();
		payOrderConsoleService.queryByCondtion(page);
		return JsonResult.success(page);
	}

	@PostMapping(MODEL + "/list/condition.json")
	@Function("payorder.query")
	@ResponseBody
	public JsonResult<List<Map<String, Object>>> indexCondtion() {
		List<Map<String, Object>> list = AnnotationUtil.getInstance().getAnnotations(Query.class, PayOrderQuery.class);
		return JsonResult.success(list);
	}

	@PostMapping(MODEL + "/excel/export.json")
	@Function("payorder.export")
	@ResponseBody
	public JsonResult<String> export(HttpServletResponse response,PayOrderQuery condtion) {
		String excelTemplate ="excelTemplates/admin/payorder/payorder_collection_template.xls";
		PageQuery<PayOrder> page = condtion.getPageQuery();
		//取出全部符合条件的
		page.setPageSize(Integer.MAX_VALUE);
		page.setPageNumber(1);
		page.setTotalRow(Integer.MAX_VALUE);
		List<PayOrderExcelExportData> payOrders =payOrderConsoleService.queryExcel(page);
		try(InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(excelTemplate)) {
	        if(is==null) {
	        	throw new PlatformException("模板资源不存在："+excelTemplate);
	        }
	        FileItem item = fileService.createFileTemp("payorder_collection.xls");
	        OutputStream os = item.openOutpuStream();
	        Context context = new Context();
            context.putVar("payOrders", payOrders);
            JxlsHelper.getInstance().processTemplate(is, os, context);
            //下载参考FileSystemContorller
            return  JsonResult.success(item.getPath());
	    } catch (IOException e) {
			throw new PlatformException(e.getMessage());
		}
		
	}
	
	
	
	
	

}
